package models

import (
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
	"log"
	"time"
)

type Article struct {
	Id      string `bson:"_id,omitempty"`
	Title   string
	Summary string
	Body    string
	Uid     string
	RNum    int64
	GNum    string
	IniTime time.Time
}

//添加一篇文章
func (this *Article) Add() error {
	defer close()
	defer exption()
	log.Println("开始写入Article")
	var c = open("Article")
	err := c.Insert(this)
	return err
}

//更新一篇文章
func (this *Article) Update() error {
	defer close()
	defer exption()
	log.Println("开始更新Article")
	var c = open("Article")
	//只修改部分需要用$set否则会覆盖
	err := c.Update(bson.M{"_id": this.Id}, bson.M{"$set": bson.M{
		"title":   this.Title,
		"body":    this.Body,
		"summary": this.Summary,
		"gnum":    this.GNum,
	}})
	return err
}

//更新阅读次数
func (this *Article) ChangeRNum() error {
	defer close()
	defer exption()
	log.Println("开始更新Article")
	var c = open("Article")
	//只修改部分需要用$set否则会覆盖
	//this.Get()
	err := c.Update(bson.M{"_id": this.Id}, bson.M{"$set": bson.M{
		"rnum": this.RNum,
	}})
	return err
}

//读取所有文章
func (this *Article) QueryAll() []Article {
	defer close()
	defer exption()
	log.Println("读取所有Article")
	var c = open("Article")
	result := make([]Article, 1)
	err := c.Find(bson.M{}).Sort("-_id").All(&result)
	if err != nil {
		log.Println(err)
	}
	//log.Println(result);
	return result
}

//读取所有文章
func (this *Article) Seach(key string, size int) []Article {
	defer close()
	defer exption()
	log.Println("读取所有Article")
	var c = open("Article")
	result := make([]Article, 1)
	err := c.Find(bson.M{"title": bson.M{"$regex": key}}).Sort("-_id").Limit(size).All(&result)
	if err != nil {
		log.Println(err)
	}
	//log.Println(result);
	return result
}

func (this *Article) Queryg(groupid string) []Article {
	defer close()
	defer exption()
	log.Println("读取所有Article")
	var c = open("Article")
	result := make([]Article, 1)
	var query *mgo.Query
	if groupid == "" {
		query = c.Find(bson.M{}) //.Select(bson.M{"title": 1, "body": 0, "summary": 0})
	} else {
		query = c.Find(bson.M{"gnum": groupid}) //.Select(bson.M{"title": 1, "body": 0, "summary": 0})
	}
	err = query.Sort("-_id").All(&result)
	if err != nil {
		log.Println(err)
	}
	return result
}

//读取所有文章
func (this *Article) Query(maxid string, groupid string, index int, size int) ([]Article, int) {
	defer close()
	defer exption()
	log.Println("读取所有Article")
	var c = open("Article")
	result := make([]Article, 1)
	var query *mgo.Query
	if groupid == "" {
		query = c.Find(bson.M{"_id": bson.M{"$gt": maxid}})
	} else {
		query = c.Find(bson.M{"_id": bson.M{"$gt": maxid}, "gnum": groupid})
	}

	count, err := query.Count()
	if err != nil {
		log.Println(err)
	}
	err = query.Skip(index * size).Limit(size).Sort("-_id").All(&result)
	if err != nil {
		log.Println(err)
	}
	//log.Println(result);
	return result, count
}

//读取一篇文章
func (this *Article) Get() error {
	defer close()
	defer exption()
	log.Println("读取一条Article")
	var c = open("Article")
	err := c.Find(bson.M{"_id": this.Id}).One(this)
	return err
}
